use strict;
use warnings;
use Time::HiRes qw(sleep);
use Test::WWW::Selenium;
use Test::More "no_plan";
use Test::Exception;
use Net::IP;

# Create a function which uses curl to get the current IP address.
# If check_ip() fails then it runs again
# If curl succeeds then the function returns the current IP address

sub check_ip {
    eval{
        my $whatismyip = `curl whatismyip.org`;
        # whatismyip.org sometimes doesn't return a value, so call it until it does
        while (length($whatismyip) < 8){
            $whatismyip = `curl whatismyip.org`;
        }    
        my $ip_test = new Net::IP(`curl whatismyip.org`);
        return $ip_test->ip();
        };
    if ($@){
        sleep(300);
        check_ip();
    };
}


sub change_ip{
    my $sel;
	my $port = 4444;
	my $password = "add your password here";
	$password = shift if @_;
	my $browser_url = "http://192.168.1.1";
	$browser_url = ("http://" . shift) if @_;
	print "\n The Actiontec router's admin page is at $browser_url .\n";
	my $ip_current = new Net::IP(`curl whatismyip.org`) or die (Net::IP::Error());
	eval {$sel = Test::WWW::Selenium->new( host => "localhost", 
                                    port => $port, 
                                    browser => "*chrome", 
                                    browser_url => $browser_url );
    };
    if ($@){
        print "This module requires that a Selenium server listen at port 4444.\nPlease start your Selenium server.\n";
        exit;
    }
	my $ip_original_text = $ip_current->ip();
	my $ip_current_text = $ip_original_text;
	while ($ip_current_text eq $ip_original_text){
		$sel->open_ok("/");
		$sel->focus_ok("user_name");
		$sel->type_ok("user_name", "admin");
		$sel->focus_ok("pass2");
		for (my $count = 0; $count < length($password); $count++) { 
		    $sel->key_down_ok("pass2", substr($password, $count, 1));
		    $sel->key_press_ok("pass2", substr($password, $count, 1));
		    $sel->key_up_ok("pass2", substr($password, $count, 1));
		}
		$sel->click_ok("//input[\@value='  OK  ']");
		$sel->wait_for_page_to_load_ok("30000");
		$sel->click_ok("MyNtwk_top");
		$sel->wait_for_page_to_load_ok("30000");
		$sel->click_ok("link=Network Connections");
		$sel->wait_for_page_to_load_ok("30000");
		$sel->click_ok("link=Broadband Connection (Coax)");
		$sel->wait_for_page_to_load_ok("30000");
		$sel->click_ok("//input[\@value='  Settings  ']");
		$sel->wait_for_page_to_load_ok("30000");
		$sel->click_ok("//input[\@value='  Release  ']");
		$sel->wait_for_page_to_load_ok("30000");
		$sel->click_ok("//input[\@value='  Apply  ']");
		# wait 5 minutes so as not to prematurely trigger renewal of DHCP lease
		# 1 ms wait for page to load shows that wait has begun
		sleep(300);
		# 1 ms wait for page to load indicates in terminal window that wait is 
		# complete
		$ip_current->set(check_ip());
		# 2 ms wait for page to load indicates in terminal window that check_ip is 
		# complete
		$ip_current_text = "";
		while (length($ip_current_text) < 8) {
    	        $ip_current_text = `curl whatismyip.org`;
    	}
    	if ($ip_original_text eq $ip_current_text) {
        	print "The two IP values $ip_original_text and $ip_current_text are equal\n.";
        	print "There has been a logical error in the execution of this program. Please try again.\n";
    	}  else {
        	print "The old ip value $ip_original_text and the new ip value $ip_current_text are not equal\n";
    	}
	}
}


change_ip("add your password here", "192.168.1.1");
